using System;
using System.Collections.Generic;
using System.Text;
using FlatRedBall;
using FlatRedBall.Input;
using FlatRedBall.AI.Pathfinding;
using FlatRedBall.Graphics.Animation;
using FlatRedBall.Graphics.Particle;

using FlatRedBall.Graphics.Model;
using FlatRedBall.Math.Geometry;
using FlatRedBall.Math.Splines;

using Cursor = FlatRedBall.Gui.Cursor;
using GuiManager = FlatRedBall.Gui.GuiManager;
using FlatRedBall.Localization;

#if FRB_XNA || SILVERLIGHT
using Keys = Microsoft.Xna.Framework.Input.Keys;
using Vector3 = Microsoft.Xna.Framework.Vector3;
using Texture2D = Microsoft.Xna.Framework.Graphics.Texture2D;
#endif

namespace NereusGame.Screens
{
	public partial class GameMode
	{
        private const int MAX_BUTTONS = 3;  // max number of buttons
        private List<Sprite> buttonsN;      // list of normal buttons
        private List<Sprite> buttonsH;      // list of selected buttons
        private int  selector;              // indiactes which buttons are selected
        private Xbox360GamePad gamePad;     // assign gamepad
        private Xbox360GamePad keyboard;    // assing keyboard
            
		void CustomInitialize()
		{
            ResetCamera();
            InitializeController();
            InitializeKeyboard();
            Initialize();
            

		}

		void CustomActivity(bool firstTimeCalled)
		{

            MoveSelector();
            Select();
		}

		void CustomDestroy()
		{
            for (int i = 0; i < MAX_BUTTONS; i++)
            {
                SpriteManager.RemoveSprite(buttonsN[i]);
                SpriteManager.RemoveSprite(buttonsH[i]);
            }

		}

        static void CustomLoadStaticContent(string contentManagerName)
        {


        }

        // set up controller
        protected void InitializeController()
        {
            gamePad = InputManager.Xbox360GamePads[0]; // assign gamepad

        }

        // keybindings for this screen
        private void InitializeKeyboard()
        {

            // map keys to keyboard.
            FlatRedBall.Input.InputManager.Xbox360GamePads[1].CreateDefaultButtonMap();
            FlatRedBall.Input.InputManager.Xbox360GamePads[1].ButtonMap.RightAnalogDown = Keys.K;
            FlatRedBall.Input.InputManager.Xbox360GamePads[1].ButtonMap.RightAnalogUp = Keys.I;
            FlatRedBall.Input.InputManager.Xbox360GamePads[1].ButtonMap.RightAnalogLeft = Keys.J;
            FlatRedBall.Input.InputManager.Xbox360GamePads[1].ButtonMap.RightAnalogRight = Keys.L;
            FlatRedBall.Input.InputManager.Xbox360GamePads[1].ButtonMap.DPadDown = Keys.Down;
            FlatRedBall.Input.InputManager.Xbox360GamePads[1].ButtonMap.DPadUp = Keys.Up;
            FlatRedBall.Input.InputManager.Xbox360GamePads[1].ButtonMap.DPadLeft = Keys.Left;
            FlatRedBall.Input.InputManager.Xbox360GamePads[1].ButtonMap.DPadRight = Keys.Right;
            FlatRedBall.Input.InputManager.Xbox360GamePads[1].ButtonMap.A = Keys.Enter;
            FlatRedBall.Input.InputManager.Xbox360GamePads[1].ButtonMap.B = Keys.Back;
            keyboard = InputManager.Xbox360GamePads[1]; // assign gamepad

        }

        private void Initialize()
        {
            buttonsN = new List<Sprite>();
            buttonsH = new List<Sprite>();

            Vector3 pos = new Vector3(0, SpriteManager.Camera.YEdge / 10f, 0);
            for (int i = 0; i < MAX_BUTTONS; i++ )
            {
                buttonsN.Add(new Sprite());
                buttonsH.Add(new Sprite());
                SpriteManager.AddSprite(buttonsN[i]);
                SpriteManager.AddSprite(buttonsH[i]);

                buttonsH[i].ScaleY = buttonsN[i].ScaleY = 1.5f;
                buttonsH[i].ScaleX = buttonsN[i].ScaleX = buttonsN[i].ScaleY * 5f;
                buttonsN[i].Position = buttonsH[i].Position = pos;
                pos.Y -= buttonsH[i].ScaleY * 3f;
                buttonsH[i].Visible = false;

            }

            // set textures
            buttonsN[0].Texture = SinglePlayerN;
            buttonsN[1].Texture = CoUpN;
            buttonsN[2].Texture = ArenaN;
            buttonsH[0].Texture = SinglePlayerH;
            buttonsH[1].Texture = CoUpH;
            buttonsH[2].Texture = ArenaH;
            selector = 0;
        }

        // navigates through buttons
        private void MoveSelector()
        {
            if (gamePad.ButtonPushed(Xbox360GamePad.Button.DPadDown) || gamePad.LeftStick.AsDPadPushed(Xbox360GamePad.DPadDirection.Down) ||
                keyboard.ButtonPushed(Xbox360GamePad.Button.DPadDown) || keyboard.LeftStick.AsDPadPushed(Xbox360GamePad.DPadDirection.Down))
            {
                buttonsN[selector].Visible = true;
                buttonsH[selector].Visible = false;

                selector++;
                if (selector > buttonsN.Count - 1)
                    selector = 0;
            }
            else if (gamePad.ButtonPushed(Xbox360GamePad.Button.DPadUp) || gamePad.LeftStick.AsDPadPushed(Xbox360GamePad.DPadDirection.Up) ||
                keyboard.ButtonPushed(Xbox360GamePad.Button.DPadUp) || keyboard.LeftStick.AsDPadPushed(Xbox360GamePad.DPadDirection.Up))
            {
                buttonsN[selector].Visible = true;
                buttonsH[selector].Visible = false;
                selector--;
                if (selector < 0)
                    selector = buttonsN.Count - 1;
            }

            buttonsN[selector].Visible = false;
            buttonsH[selector].Visible = true;
        }

        // selects action depedning on user input.
        private void Select()
        {
            if (gamePad.ButtonReleased(Xbox360GamePad.Button.A) || keyboard.ButtonReleased(Xbox360GamePad.Button.A))
            {
                // single player mode
                if (selector == 0)
                    Game1.mGameMode = Game1.GameMode.SinglePlayer;
                // co up mode.
                else if (selector == 1)
                    Game1.mGameMode = Game1.GameMode.CoOp;
                else if (selector == 2)
                    Game1.mGameMode = Game1.GameMode.Arena;
                MoveToScreen(typeof(LoadScreen).FullName);
            }

            // cancle and go back to previous screen
            if (gamePad.ButtonPushed(Xbox360GamePad.Button.B) || keyboard.ButtonPushed(Xbox360GamePad.Button.B))
            {
                ScreenManager.NavigateBack();
            }
        }


        private void ResetCamera()
        {
            // reset camera position
            SpriteManager.Camera.Position = SpriteManager.Camera.Velocity = SpriteManager.Camera.Acceleration = Vector3.Zero;
            SpriteManager.Camera.Position.Z = 40f;
        }


	}
}
